<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" ref="dialogForm" :disabled="mode==='show'" label-width="100px" label-position="left">
            <el-form-item label="参数编码" prop="configCode">
                <el-input v-model="form.configCode" :disabled="mode!=='add'" clearable></el-input>
            </el-form-item>
            <el-form-item label="参数名称" prop="configName">
                <el-input v-model="form.configName" clearable></el-input>
            </el-form-item>
            <el-form-item label="参数值" prop="configValue">
                <el-input v-model="form.configValue" clearable></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <sc-select v-model="form.status" dict="ENABLE_STATUS" filterable style="width: 100%;"></sc-select>
            </el-form-item>
            <el-form-item label="备注" prop="configNote">
                <el-input v-model="form.configNote" type="textarea" maxlength="255" show-word-limit/>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false">取 消</el-button>
            <el-button type="primary" v-if="mode!=='show'" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
export default {
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '新增运营参数',
                edit: '编辑运营参数'
            },
            form: {
                id: "",
                configCode: "",
                configName: "",
                configValue: "",
                configNote: "",
                status: "ENABLED"
            },
            rules: {
                configCode: [
                    {required: true, message: '请输入参数编码'}
                ],
                configName: [
                    {required: true, message: '请输入参数名称'}
                ],
                configValue: [
                    {required: true, message: '请输入参数值'}
                ]
            },
            visible: false,
            isSaveing: false
        }
    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    const res = this.mode === 'add' ? await this.$API.system.config.post(this.form) : await this.$API.system.config.put(this.form);
                    this.isSaveing = false;
                    if (res.code === 200) {
                        this.$emit('success', this.form, this.mode)
                        this.visible = false;
                        this.$message.success("操作成功")
                    } else {
                        await this.$alert(res.msg, "提示", {type: 'error'})
                    }
                }
            })
        },
        //表单注入数据
        setData(data) {
            this.form.id = data.id
            this.form.configCode = data.configCode
            this.form.configName = data.configName
            this.form.configValue = data.configValue
            this.form.configNote = data.configNote
            this.form.status = data.status
        }
    }
}
</script>

<style>
</style>
